﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using System.Data;
using System.IO;
using System.Web;

namespace Portal.Web.Utility
{
    public class WebConfig
    {
        public static string cmdCode = "10004301!@#";
        public static string XmlSiteConfigFile
        {
            get
            {
                string ItemXmlSiteConfigFile = (string)System.Configuration.ConfigurationManager.AppSettings["XmlSiteConfigFile"];
                return ItemXmlSiteConfigFile;
            }
        }

        public static string WorkFlowId
        {
            get
            {
                string _WorkFlowId = (string)System.Configuration.ConfigurationManager.AppSettings["WorkFlowId"];
                return _WorkFlowId;
            }
        }
        
        public static string PublicUrl
        {
            get
            {
                string publicurl = (string)System.Configuration.ConfigurationManager.AppSettings["publicurl"];
                return publicurl;
            }
        }

        public static string GetDataLanguage(SiteParam OSiteParam)
        {
            string DataLanguage = GetConfigFromXml(OSiteParam, "DataLanguage");
            if (string.IsNullOrEmpty(DataLanguage)) DataLanguage = "vi";
            return DataLanguage;
        }

        public static string GetAssetCode(SiteParam OSiteParam)
        {
            string AssetCode = GetConfigFromXml(OSiteParam, "DefaultAssetCode");
            return AssetCode;
        }

        public static string BaseSiteUrl
        {
            get
            {
                HttpContext context = HttpContext.Current;
                string baseUrl = context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/');
                return baseUrl;
            }
        }


        public static string ConvertUrl(string SiteId, string Url)
        {
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string ConvertUrl = WebConfig.GetConfigFromXml(OSiteParam, "ConvertUrl");
            string[] ItemConvertUrls = ConvertUrl.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            for (int iIndex = 0; iIndex < ItemConvertUrls.Length; iIndex++)
            {
                //Url = Url.Replace("192.168.1.2/", BaseSiteUrl + "/");
                Url = Url.Replace("" + ItemConvertUrls[iIndex] + "/", BaseSiteUrl + "/");
            }
            //Url = Url.Replace("117.3.4.207/", BaseSiteUrl + "/");
            return Url;
        }

        public static string GetPortalSystemsBussinessService(SiteParam OSiteParam)
        {
            string Id = OSiteParam.PortalSystemsBussiness;// WebConfig.GetConfigFromXml(OSiteParam, "PortalSystemsBussinessId");
            return Id;
        }

        public static string GetWebPartLayout(SiteParam OSiteParam)
        {
            string WebPartLayout = GetConfigFromXml(OSiteParam, "WebPartLayout");
            WebPartLayout = WebConfig.ConvertPathRoot(OSiteParam, WebPartLayout);
            return WebPartLayout;
        }

        public static string GetTempPath(SiteParam OSiteParam)
        {
            string TempPath = GetConfigFromXml(OSiteParam, "TempPath");
            if (string.IsNullOrEmpty(TempPath))
            {
                TempPath = DataPathRoot(OSiteParam);
                TempPath = TempPath + "\\temp";
                System.IO.Directory.CreateDirectory(TempPath);
                TempPath = TempPath + "\\" + OSiteParam.SiteCode;
                System.IO.Directory.CreateDirectory(TempPath);
            }
            else
            {
                System.IO.Directory.CreateDirectory(TempPath);
            }
            return TempPath;
        }


        public static string GetTempHttpRoot(SiteParam OSiteParam)
        {
            string TempHttpPath = GetConfigFromXml(OSiteParam, "TempHttpPath");
            if (string.IsNullOrEmpty(TempHttpPath))
            {
                TempHttpPath = HttpDataRoot(OSiteParam);
                TempHttpPath = TempHttpPath + "/temp/" + OSiteParam.SiteCode;
            }
            return TempHttpPath;
        }

        public static string GetWebPartLayoutIconRoot(SiteParam OSiteParam)
        {
            string WebPartLayoutIconRoot = GetConfigFromXml(OSiteParam, "WebPartLayoutIconRoot");
            WebPartLayoutIconRoot =  WebConfig.ConvertHttpRoot(OSiteParam, WebPartLayoutIconRoot);
            WebPartLayoutIconRoot = ConvertUrl(OSiteParam.SiteId, WebPartLayoutIconRoot);
            return WebPartLayoutIconRoot;
        }

        public static string GetButtonIconRoot(SiteParam OSiteParam)
        {
            string ButtonIconRoot = GetConfigFromXml(OSiteParam, "ButtonIconRoot");
            ButtonIconRoot = WebConfig.ConvertHttpRoot(OSiteParam, ButtonIconRoot);
            ButtonIconRoot = ConvertUrl(OSiteParam.SiteId, ButtonIconRoot);
            return ButtonIconRoot;
        }

        public static string GetIconRoot(SiteParam OSiteParam)
        {
            string IconRoot = GetConfigFromXml(OSiteParam, "IconRoot");
            IconRoot = WebConfig.ConvertHttpRoot(OSiteParam, IconRoot);
            IconRoot = ConvertUrl(OSiteParam.SiteId, IconRoot);
            return IconRoot;
        }

       
        public static string GetConfigFromXml(SiteParam OSiteParam, string Field)
        {
            //SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            string Data = "";
            //if (System.Web.HttpContext.Current.Application[SiteId + Field] == null)
            {
                string Path = OSiteParam.XmlConfigFile;// OSiteParam.PathRoot + "\\Xml\\SiteConfig.xml";
                if (!File.Exists(Path))
                {
                    throw new Exception("Đường dẫn cấu hình không hợp lệ. Không tìm thấy file cấu hình [" + Path + "]");
                }
                DataSet ds = new DataSet();
                ds.ReadXml(Path);
                try
                {
                    Data = (string)ds.Tables[0].Rows[0][Field];
                    return Data;
                    //try
                    //{
                    //    System.Web.HttpContext.Current.Application[SiteId + Field] = Data;
                    //}
                    //catch { }
                }
                catch
                {
                    return "";
                }
            }
            //return (string)System.Web.HttpContext.Current.Application[SiteId + Field];
        }

        public static string DataPathRoot(SiteParam OSiteParam)
        {
            string ItemUserDataPathRoot = GetConfigFromXml(OSiteParam, "UserDataPathRoot");
            return ItemUserDataPathRoot;
        }

        public static string UserDataPathRoot(SiteParam OSiteParam, string AssetCode, string Course, string StudentId)
        {
            //if (string.IsNullOrEmpty(Course)) throw new Exception("Chưa xác định khóa");
            string ItemUserDataPathRoot = GetConfigFromXml(OSiteParam, "UserDataPathRoot");
            System.IO.Directory.CreateDirectory(ItemUserDataPathRoot + "\\" + AssetCode);
            System.IO.Directory.CreateDirectory(ItemUserDataPathRoot + "\\" + AssetCode + "\\" + Course);
            System.IO.Directory.CreateDirectory(ItemUserDataPathRoot + "\\" + AssetCode + "\\" + Course + "\\" + StudentId);
            ItemUserDataPathRoot = ItemUserDataPathRoot + "\\" + AssetCode + "\\" + Course + "\\" + StudentId;
            return ItemUserDataPathRoot;
        }

        public static string HttpDataRoot(SiteParam OSiteParam)
        {
            string ItemHttpUserDataRoot = GetConfigFromXml(OSiteParam, "HttpUserDataRoot");
            return ItemHttpUserDataRoot;
        }


        public static string HttpUserDataRoot(SiteParam OSiteParam, string AssetCode, string Course, string StudentId)
        {
            string ItemHttpUserDataRoot = GetConfigFromXml(OSiteParam, "HttpUserDataRoot");
            if (string.IsNullOrEmpty(Course))
            {
                Course = "admin";
            }
            ItemHttpUserDataRoot = ItemHttpUserDataRoot + "/" + AssetCode + "/" + Course + "/" + StudentId;
            return ItemHttpUserDataRoot;
        }


        public static string ConvertPathRoot(SiteParam OSiteParam, string PathRoot)
        {
            PathRoot = PathRoot.Replace("{PathRoot}", OSiteParam.PathRoot);
            return PathRoot;
        }

        public static string ConvertHttpRoot(SiteParam OSiteParam, string PathRoot)
        {
            PathRoot = PathRoot.Replace("{HttpRoot}", OSiteParam.HttpRoot);
            return PathRoot;
        }
    }
}
